//
//  NewLoveMarketingTopCarouselCell.m
//  HCNewGoldFinger
//
//  Created by 刘琛 on 2018/4/10.
//  Copyright © 2018年 zjhcsoftios. All rights reserved.
//

#import "NewLoveMarketingTopCarouselCell.h"
#import "ZFChart.h"
#import <BlocksKit/UIView+BlocksKit.h>
#import "UIColor+EX.h"

@interface NewLoveMarketingTopCarouselCell()<ZFGenericChartDataSource, ZFWaveChartDelegate,ZFBarChartDelegate,ZFLineChartDelegate>

@property (weak, nonatomic) IBOutlet UILabel *title;
@property (weak, nonatomic) IBOutlet UIView *chartBackView;

@property (nonatomic, strong) ZFWaveChart *waveChart;

@property (nonatomic, strong) ZFBarChart *barChart;

@property (nonatomic, strong) ZFLineChart *lineChart;

@property (nonatomic, strong) NSString *chartType;

@property (strong, nonatomic) UIImageView *noDataImage;
 
@end

@implementation NewLoveMarketingTopCarouselCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(chartBackViewAction)];
    _noDataImage = [[UIImageView alloc] initWithFrame:CGRectMake((SCREEN_WIDTH - 80) / 2, (115 - 80) / 2, 80, 80)];
    [_noDataImage setImage:[self getImageFromBundle:@"无数据"]];
    
    [self.chartBackView addGestureRecognizer:tap];
}

- (void)setModel:(NewHomeCarouselModel *)model {
    _model = model;
    
    NSString *title = [NSString stringWithFormat:@"%@: ",model.label];
    NSString *content = model.value;
    NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@%@",title,content]];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"888888"] range:NSMakeRange(0, title.length - 1)];
    [str addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHexString:@"000"] range:NSMakeRange(title.length,  str.length - title.length)];
    [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:12] range:NSMakeRange(0, str.length)];
    _title.attributedText = str;
    
    [self reloadCharts];

}
- (void)chartBackViewAction {
    if ([_delegate respondsToSelector:@selector(newLoveMarketingTopCarouselCell:model:)]) {
        [_delegate newLoveMarketingTopCarouselCell:self model:self.model];
    }

}

//判断视图类型进行添加等

- (void)reloadCharts {
    
    if (_model.yaxis.count == 0) {
        [self removeAllViews:_chartBackView];
        [_chartBackView addSubview:_noDataImage];
    }else{
        if ([_model.chartType isEqualToString:@"bar"]) {
            if ([_model.chartType isEqualToString:_chartType]) {
                if (_barChart.superview == _chartBackView) {
                    
                }else {
                    [self removeAllViews:_chartBackView];
                    [_chartBackView addSubview:self.barChart];
                }

                [self.barChart strokePath];
            }else {
                [self removeAllViews:_chartBackView];
                [_chartBackView addSubview:self.barChart];
                [self.barChart strokePath];
                _chartType = _model.chartType;
            }
        }else if ([_model.chartType isEqualToString:@"line"]) {
            if ([_model.chartType isEqualToString:_chartType]) {
                if (_lineChart.superview == _chartBackView) {
                   
                }else {
                    [self removeAllViews:_chartBackView];
                    [_chartBackView addSubview:self.lineChart];
                }
                [self.lineChart strokePath];
            }else {
                [self removeAllViews:_chartBackView];
                [_chartBackView addSubview:self.lineChart];
                [self.lineChart strokePath];
                _chartType = _model.chartType;
            }
        }else if ([_model.chartType isEqualToString:@"area"]) {
            if ([_model.chartType isEqualToString:_chartType]) {
                if (_waveChart.superview == _chartBackView) {
                    
                }else {
                    [self removeAllViews:_chartBackView];
                    [_chartBackView addSubview:self.waveChart];
                }
                [self.waveChart strokePath];
            }else {
                [self removeAllViews:_chartBackView];
                [_chartBackView addSubview:self.waveChart];
                [self.waveChart strokePath];
                _chartType = _model.chartType;
            }
        }
    }
}


#pragma mark 波形图
- (ZFWaveChart *)waveChart {
    if (!_waveChart) {
        _waveChart = [[ZFWaveChart alloc] initWithFrame:CGRectMake(-45, -30, SCREEN_WIDTH - 10 + 50, 125 + 30 - 10)];
        _waveChart.dataSource = self;
        _waveChart.delegate = self;
//        _waveChart.yLineSectionCount
//        self.waveChart.topicLabel.text = @"xx小学各年级人数";
//      self.waveChart.unit = @"人";
      self.waveChart.isShowYLineSeparate = NO;
//      self.waveChart.pathColor = ZFGrassGreen;
        _waveChart.pathLineColor = [UIColor colorWithHexString:@"3BBFBF"];
        _waveChart.topicLabel.textColor = ZFPurple;
        _waveChart.xAxisColor = [UIColor colorWithHexString:@"F5F6F8"];
        _waveChart.yAxisColor = ZFClear;
        _waveChart.axisLineNameColor = [UIColor colorWithHexString:@"C5C5C5"];
        _waveChart.axisLineValueColor = [UIColor colorWithHexString:@"C5C5C5"];
//        _waveChart.isAnimated = NO;
//      self.waveChart.isResetAxisLineMinValue = YES;
//      self.waveChart.isShowAxisLineValue = NO;
//      self.waveChart.isShadowForValueLabel = NO;
        _waveChart.valuePosition = kChartValuePositionOnTop;
        _waveChart.valueLabelPattern = kPopoverLabelPatternBlank;
//        _waveChart.valueLabelShadowColor = [UIColor colorWithHexString:@"169BD5"];
        _waveChart.valueTextColor = [UIColor colorWithHexString:@"169BD5"];
//      self.waveChart.wavePatternType = kWavePatternTypeForSharp;
//      self.waveChart.valueLabelToWaveLinePadding = 20.f;
//      [self.waveChart strokePath];
//      [self.chartBackView addSubview:self.waveChart];
    }
    return _waveChart;
}

//波形图渐变色
- (ZFGradientAttribute *)gradientColorInWaveChart:(ZFWaveChart *)waveChart {
    
    ZFGradientAttribute * gradientAttribute = [[ZFGradientAttribute alloc] init];
    gradientAttribute.colors = @[(id)[UIColor colorWithHexString:@"3BBFBF"].CGColor, (id)[UIColor colorWithHexString:@"CFE2F3"].CGColor];
    gradientAttribute.locations = @[@(0.0), @(1)];
    return gradientAttribute;
}

#pragma mark 柱状图    

- (ZFBarChart *)barChart {
    if (!_barChart) {
        _barChart = [[ZFBarChart alloc] initWithFrame:CGRectMake(-45, -30, SCREEN_WIDTH - 10 + 50, 125 + 30 - 10)];
        _barChart.dataSource = self;
        _barChart.delegate = self;
//        _barChart.topicLabel.text = @"xx小学各年级人数";
//        _barChart.unit = @"人";
//        _barChart.isAnimated = NO;
//        _barChart.isResetAxisLineMinValue = YES;
//        _barChart.isResetAxisLineMaxValue = YES;
//        _barChart.isShowAxisLineValue = YES;
//        self.barChart.valueLabelPattern = kPopoverLabelPatternBlank;
//        _barChart.isShowXLineSeparate = NO;
        _barChart.isShowYLineSeparate = NO;
        _barChart.valueLabelPattern = kPopoverLabelPatternBlank;
//        self.barChart.topicLabel.textColor = ZFWhite;
//        self.barChart.unitColor = ZFWhite;
//        self.barChart.xAxisColor = ZFWhite;
//        self.barChart.yAxisColor = ZFWhite;
        self.barChart.xAxisColor = [UIColor colorWithHexString:@"C5C5C5"];
        self.barChart.yAxisColor = ZFClear;
        self.barChart.axisLineNameColor = [UIColor colorWithHexString:@"C5C5C5"];
        self.barChart.axisLineValueColor = [UIColor colorWithHexString:@"C5C5C5"];
//        self.barChart.backgroundColor = ZFPurple;
//        self.barChart.isShowAxisArrows = NO;
        _barChart.separateLineStyle = kLineStyleDashLine;
//        _barChart.isMultipleColorInSingleBarChart = YES;
//        self.barChart.separateLineDashPhase = 0.f;
//        self.barChart.separateLineDashPattern = @[@(5), @(5)];
    }
    return _barChart;
}

- (id)valueTextColorArrayInBarChart:(ZFBarChart *)barChart {
    return (id)[UIColor colorWithHexString:@"169BD5"];
}

- (CGFloat)barWidthInBarChart:(ZFBarChart *)barChart {
    return 25;
}

- (CGFloat)paddingForGroupsInBarChart:(ZFBarChart *)barChart {
    return 10;
}

- (NSArray *)gradientColorArrayInBarChart:(ZFBarChart *)barChart{
    
    ZFGradientAttribute * gradientAttribute = [[ZFGradientAttribute alloc] init];
    gradientAttribute.colors = @[(id)[UIColor colorWithHexString:@"169BD5"].CGColor, (id)[UIColor colorWithHexString:@"169BD5"].CGColor];
    gradientAttribute.locations = @[@(0.0), @(1)];
    
    return [NSArray arrayWithObjects:gradientAttribute, nil];
}

#pragma mark 折线图

- (ZFLineChart *)lineChart {
    if (!_lineChart) {
        _lineChart = [[ZFLineChart alloc] initWithFrame:CGRectMake(-45, -30, SCREEN_WIDTH - 10 + 50, 125 + 30 - 10)];
        _lineChart.dataSource = self;
        _lineChart.delegate = self;
//        _lineChart.topicLabel.text = @"xx小学各年级男女人数";
//        _lineChart.unit = @"人";
//        _lineChart.topicLabel.textColor = ZFPurple;
//        _lineChart.isResetAxisLineMinValue = YES;
//        _lineChart.isAnimated = NO;
//        _lineChart.valueLabelPattern = kPopoverLabelPatternBlank;
//        _lineChart.isShowXLineSeparate = YES;
        _lineChart.isShowYLineSeparate = NO;
        _lineChart.xAxisColor = [UIColor colorWithHexString:@"F5F6F8"];
        _lineChart.yAxisColor = ZFClear;
        _lineChart.axisLineNameColor = [UIColor colorWithHexString:@"C5C5C5"];
        _lineChart.axisLineValueColor = [UIColor colorWithHexString:@"C5C5C5"];
        _lineChart.linePatternType = kLinePatternTypeSharp;
        _lineChart.valueLabelPattern = kPopoverLabelPatternBlank;
//        _lineChart.valuePosition = kChartValuePositionOnTop;
        _lineChart.valueLabelShadowColor = [UIColor colorWithHexString:@"169BD5"];
        _lineChart.lineStyle = kLineStyleRealLine;
    }
    return _lineChart;
}

- (NSArray<ZFGradientAttribute *> *)gradientColorArrayInLineChart:(ZFLineChart *)lineChart{
    ZFGradientAttribute * gradientAttribute = [[ZFGradientAttribute alloc] init];
    gradientAttribute.colors = @[(id)[UIColor colorWithHexString:@"169BD5"].CGColor, (id)[UIColor colorWithHexString:@"8AD3FF"].CGColor];
    gradientAttribute.locations = @[@(0.1), @(0.22)];
    gradientAttribute.startPoint = CGPointMake(0.5, 0);
    gradientAttribute.endPoint = CGPointMake(0.5, 1);
    
    return [NSArray arrayWithObjects:gradientAttribute, nil];
}

- (NSArray *)colorArrayInGenericChart:(ZFGenericChart *)chart {
    return @[(id)[UIColor colorWithHexString:@"169BD5"]];
}

#pragma mark 图表代理
- (NSArray *)valueArrayInGenericChart:(ZFGenericChart *)chart{
    return _model.yaxis;
}

- (NSArray *)nameArrayInGenericChart:(ZFGenericChart *)chart{
    return _model.xaxis;
}

- (void)removeAllViews:(UIView *)fatherView {
    
    for(UIView *view in [fatherView subviews])
    {
        [view removeFromSuperview];
    }
}

- (UIImage *)getImageFromBundle:(NSString *) imageName {
    
    NSBundle *bundle = [NSBundle bundleWithURL:[[NSBundle bundleForClass:[self class]] URLForResource:@"NewMarketLib" withExtension:@"bundle"]];
    return [UIImage imageNamed:imageName inBundle:bundle compatibleWithTraitCollection:nil];
}

@end
